/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*!
 * @file wdg_driver.c
 * @brief This file defines wdg driver functions
 */

/*******Includes***************************************************************/
#include <stdbool.h>
#include <stddef.h>
#include "wdg_reg_access.h"

/*******Definitions************************************************************/

/*******Variables**************************************************************/
/*! @brief Table of save base addresses for WDG instances */
static WDG_Type* const s_wdgRegBase[CONFIG_WDG_INSTANCE_COUNT] = CONFIG_WDG_REG_BASE_PTRS;

/*******Prototypes*************************************************************/

/*******Private Functions******************************************************/

/*******Code*******************************************************************/

/*******************************************************************************
 *** Function Name : WDG_Init
 *** Description   : WDG init
 ******************************************************************************/
errcode_t WDG_Init(uint32_t instance, const wdg_config_t* wdgConfig)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);
    ASSERT_PARAM(wdgConfig != NULL);

    WDG_Type* regBase = s_wdgRegBase[instance];
    errcode_t errCode = ERR_SUCCESS;

    if (((wdgConfig->windowModeEnable) && (wdgConfig->windowValue >= wdgConfig->timeoutValue))
            || (wdgConfig->timeoutValue <= WDG_MIN_TIMEOUT_VALUE)) {
        errCode = ERR_ERROR;
    } else {
        errCode = WDG_REG_Init(regBase, wdgConfig);
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : WDG_Deinit
 *** Description   : WDG deinit
 ******************************************************************************/
errcode_t WDG_Deinit(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];
    errcode_t errCode = ERR_SUCCESS;

    if (WDG_REG_IsUpdateEnable(regBase)) {
        WDG_REG_Deinit(regBase);
    } else {
        errCode = ERR_ERROR;
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : WDG_GetDefaultConfig
 *** Description   : Get WDG default config
 ******************************************************************************/
void WDG_GetDefaultConfig(wdg_config_t* wdgConfig)
{
    ASSERT_PARAM(wdgConfig != NULL);

    wdgConfig->clockSource = WDG_CLK_SRC_LSI;
    wdgConfig->updateEnable = true;
    wdgConfig->intEnable = false;
    wdgConfig->windowModeEnable = false;
    wdgConfig->prescalerEnable = false;
    wdgConfig->windowValue = 0U;
    wdgConfig->timeoutValue = WDG_TIMEOUT_DEFAULT_VALUE;
}

/*******************************************************************************
 *** Function Name : WDG_GetCurrentConfig
 *** Description   : Get WDG current config
 ******************************************************************************/
void WDG_GetCurrentConfig(uint32_t instance, wdg_config_t* wdgConfig)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);
    ASSERT_PARAM(wdgConfig != NULL);

    const WDG_Type* regBase = s_wdgRegBase[instance];

    *wdgConfig = WDG_REG_GetConfig(regBase);
}

/*******************************************************************************
 *** Function Name : WDG_GetCounter
 *** Description   : Get WDG current counter value
 ******************************************************************************/
uint16_t WDG_GetCounter(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    const WDG_Type* regBase = s_wdgRegBase[instance];

    return WDG_REG_GetCounter(regBase);
}

/*******************************************************************************
 *** Function Name : WDG_SetWindow
 *** Description   : Set window mode and window value
 ******************************************************************************/
errcode_t WDG_SetWindow(uint32_t instance, bool enable, uint16_t windowValue)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];
    errcode_t errCode = ERR_SUCCESS;

    if (WDG_REG_IsUpdateEnable(regBase)) {
        WDG_REG_SetWindowMode(regBase, enable);

        if (enable) {
            WDG_REG_SetWindowValue(regBase, windowValue);
        }
    } else {
        errCode = ERR_ERROR;
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : WDG_SetTimeout
 *** Description   : Set total time out
 ******************************************************************************/
errcode_t WDG_SetTimeout(uint32_t instance,  uint16_t timeoutValue)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];
    errcode_t errCode = ERR_SUCCESS;

    if (WDG_REG_IsUpdateEnable(regBase)) {
        WDG_REG_SetTimeout(regBase, timeoutValue);
    } else {
        errCode = ERR_ERROR;
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : WDG_SetIntEnable
 *** Description   : Set interrupt enable/disable
 ******************************************************************************/
errcode_t WDG_SetIntEnable(uint32_t instance,  bool enable)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];
    errcode_t errCode = ERR_SUCCESS;

    if (WDG_REG_IsUpdateEnable(regBase)) {
        WDG_REG_SetIntEnable(regBase, enable);
    } else {
        errCode = ERR_ERROR;
    }

    return errCode;
}

/*******************************************************************************
 *** Function Name : WDG_Refresh
 *** Description   : Refresh the counter
 ******************************************************************************/
void WDG_Refresh(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];

    WDG_REG_Refresh(regBase);
}

/*******************************************************************************
 *** Function Name : WDG_ClearInterruptFlag
 *** Description   : Clear the WDG interrupt flag
 ******************************************************************************/
void WDG_ClearIntFlag(uint32_t instance)
{
    ASSERT_PARAM(instance < CONFIG_WDG_INSTANCE_COUNT);

    WDG_Type* regBase = s_wdgRegBase[instance];

    WDG_REG_ClearInterruptFlag(regBase);
}

/*******EOF********************************************************************/
